
<?php
namespace ApprovalFlow\Controller;

use think\facade\Request;
use ApprovalFlow\Facade as ApprovalFlow;

class TinyflowController
{
    /**
     * 保存审批流配置
     */
    public function saveConfig()
    {
        $data = Request::post();
        
        // 转换Tinyflow格式为后端格式
        $flowData = $this->transformFromTinyflow($data);
        
        try {
            $flowId = ApprovalFlow::createFlow($flowData);
            return json([
                'code' => 200,
                'data' => ['flow_id' => $flowId],
                'message' => '保存成功'
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 500,
                'message' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * 获取审批流配置
     */
    public function getConfig($flowId)
    {
        try {
            $flowData = ApprovalFlow::getFlowDetails($flowId);
            return json([
                'code' => 200,
                'data' => $this->transformToTinyflow($flowData)
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 500,
                'message' => $e->getMessage()
            ], 500);
        }
    }

    /**
     * Tinyflow格式转后端格式
     */
    protected function transformFromTinyflow(array $tinyflowData): array
    {
        return [
            'title' => $tinyflowData['process_name'],
            'creator_id' => $tinyflowData['creator'],
            'nodes' => array_map(function($node) {
                return [
                    'name' => $node['node_name'],
                    'type' => $node['approver_type'] === 'tag' ? 1 : 2,
                    'approvers' => array_map(function($approver) {
                        return [
                            'id' => $approver['value'],
                            'type' => $approver['type'] === 'tag' ? 1 : 2
                        ];
                    }, $node['approvers']),
                    'condition' => $node['condition'] ?? null,
                    'cc' => $node['cc'] ?? []
                ];
            }, $tinyflowData['nodes'])
        ];
    }

    /**
     * 后端格式转Tinyflow格式
     */
    protected function transformToTinyflow(array $flowData): array
    {
        return [
            'process_name' => $flowData['title'],
            'creator' => $flowData['creator_id'],
            'nodes' => array_map(function($node) {
                return [
                    'node_name' => $node['name'],
                    'approver_type' => $node['type'] === 1 ? 'tag' : 'user',
                    'approvers' => array_map(function($approver) {
                        return [
                            'type' => $approver['approver_type'] === 1 ? 'tag' : 'user',
                            'value' => $approver['approver_id']
                        ];
                    }, $node['approvers']),
                    'condition' => $node['condition'] ?? null,
                    'cc' => $node['cc'] ?? []
                ];
            }, $flowData['nodes'])
        ];
    }
}
